/**
 * @Copyright ®2017 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 互联网核心系统项目
 * 创建日期 : 2017-11-13
 * 修改历史 :
 * 1. [2017-11-13]创建文件 by chenxin
 */
package com.gavinwind.uic.customer.biz.service.impl;

import com.gavinwind.uic.customer.biz.service.CustomerMngService;
import com.gavinwind.uic.customer.common.model.Customer;
import com.gavinwind.uic.customer.generated.mapper.CustomerMainMapper;
import com.gavinwind.uic.customer.generated.model.CustomerMain;
import com.sinosoft.platform.common.base.SinoBaseService;
import com.sinosoft.platform.common.config.SystemConstants;
import com.sinosoft.platform.common.exception.biz.BizRuleException;
import com.sinosoft.platform.common.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 【客户操作服务实现类】
 * @author chenxin
 * @date 17/11/13 上午8:57 
 * @version V1.0
 */
@Service
public class CustomerMngServiceImpl extends SinoBaseService implements CustomerMngService{
    @Autowired
    private CustomerMainMapper customerMainMapper;

    /**
     * 保存客户
     * @param customer
     */
    public void saveCustomerInfo(Customer customer) {
        Date currentDate = DateUtil.getCurrentDate();
        //1.校验客户信息合法完整性
        checkCustomerValid(customer);
        //2.检查客户是否已经存在
        Boolean isExistFlag =false;
        isExistFlag = isCustomerExist(customer);
        //3.检查该渠道客户信息修改权限
        //TODO
        //4.保存客户信息
        if(isExistFlag){
            //TODO
        }else {
            CustomerMain customerMain = new CustomerMain();
            customerMain.setMobile(customer.getMobile());
            customerMain.setAnnualIncome(customer.getAnnualIncome());
            customerMain.setBirthday(customer.getBirthday());
            customerMain.setCardExpiredDate(customer.getIdExpiredDate());
            customerMain.setCardNo(customer.getIdNo());
            customerMain.setCardType(customer.getIdType().code());
            customerMain.setNationality(customer.getNationality());
            customerMain.setCompanyTelephone(customer.getCompanyTelephone());
            customerMain.setCreateDate(currentDate);
            customerMain.setQq(customer.getQqNum());
            customerMain.setOperatorId(SystemConstants.SYS_OPERATOR);//TODO
            customerMain.setOccupation(customer.getOccupationCode());
            customerMain.setName(customer.getCustomerName());
            customerMain.setGender(customer.getGender());
            customerMain.setIsDelete(false);
            customerMain.setCustomerNo("1111");//TODO
            customerMain.setCustomerSource(customer.getSourceCode());
            customerMain.setHobby(customer.getHobby());
            customerMain.setFamilyAnnualIncome(customer.getFamilyAnnualIncome());
            customerMainMapper.insertSelective(customerMain);
            customer.setCustomerId(customerMain.getSid());
        }
    }

    /**
     * 判断客户是否已经存在
     * @param customer
     * @return
     */
    private Boolean isCustomerExist(Customer customer) {
        return false;
    }

    /**
     * 新增客户
     * @param customer
     * @throws BizRuleException
     */
    public void addNewCustomer(Customer customer) throws BizRuleException {

    }

    /**
     * 校验客户是否合法
     * @param customer
     */
    private void checkCustomerValid(Customer customer){
        //TODO
    }
}
